import os, sys
import logging
import logging

logging.basicConfig(level=logging.DEBUG,
                format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                datefmt='%a, %d %b %Y %H:%M:%S',
                filename='myapp.log',
                filemode='w')

mylog = logging

factor = 10

PATH = 'data/'
class Element():
    x = None
    y = None
    value = None

def Load(name):
    name = PATH + name
    if not os.path.isfile(name):
        mylog.error('Not found the csv file:%s'%(name))
        return None
    Table = {}
    f = open(name)
    #x = []
    y = []
    line = []
    for item in f:
        item = str(item).strip().split(' ')
        tmp = []
        for i in xrange(len(item)):
            ele = item[i].strip()
            if ele:
                tmp.append(ele)
            else:
                pass
        line.append(tmp)
        #print 'Line%s'%(len(tmp))
    f.close()
    x = line[0]
   
    for i in xrange(len(line)):
        if i == 0:
            pass
        else:
            y.append(line[i][0])
    #print '(%s,%s)'%(len(x),len(y))
    for i in xrange(len(x)):
        for j in xrange(len(y)):
            #print '(%s,%s)'%(x[i],y[j])
            item = Element()
            try:
                _x = int(float(x[i])*factor)
                _y = int(float(y[j])*factor)
            except:
                mylog.error('File(%s):%s,%s'%(name,x[i],y[j]))
            #mylog.info('File(%s):x:%s,y:%s'%(name,x[i],y[j]))
            data = line[j+1][i+1]
            #mylog.info('File(%s):Data:%s'%(name,data))
            if data == 'a':
                data = None
            else:
                try:
                    data = float(data)
                except:
                    mylog.error('File(%s):Invalid Data:%d'%(name,value))   
            Table[(_x,_y)] = data
    return Table

def LoadSimple(name):
    name = PATH + name
    if not os.path.isfile(name):
        mylog.error('Not found the csv file:%s'%(name))
        return None
    dic = {}
    f = open(name)
    line = []
    for item in f:
        line.append(item.strip())
    f.close()
    p = line[0].split(' ')
    for i in xrange(len(p)):
        k = int(float(p[i])*factor)
        value = line[1].split(' ')[i]
        dic[k] = float(value)
    return dic

if __name__ == '__main__':
    table = Load('jzcl1.txt')
    for k in table.keys():
        print k
    print table.get((250,250))
